import {
  getCartGory1List,
  getCartGory2List,
  getCartGory3List
} from "@/api/cartgory";
import { getAttrList } from "@/api/attr";

const state = {
  cartGory1List: [],
  cartGory2List: [],
  cartGory3List: [],
  cartGory1Id: "",
  cartGory2Id: "",
  cartGory3Id: "",
  attrList: []
};
const getters = {};

const actions = {
  async cartGory1({ commit }) {
    let res = await getCartGory1List();
    commit("CARTGORY1", res);
  },
  async cartGory2({ commit }, id) {
    let res = await getCartGory2List(id);
    commit("CARTGORY2", { res, id });
  },
  async cartGory3({ commit }, id) {
    let res = await getCartGory3List(id);
    commit("CARTGORY3", { res, id });
  },
  //获取数据
  async setAttr({ commit }) {
    let res = await getAttrList({
      category1Id: state.cartGory1Id,
      category2Id: state.cartGory2Id,
      category3Id: state.cartGory3Id
    });
    commit("SETATTR", res);
  }
};
const mutations = {
  //上来触发
  CARTGORY1(state, res) {
    state.cartGory1List = res;
  },
  //一级选中触发
  CARTGORY2(state, { res, id }) {
    state.cartGory2List = res;
    state.cartGory3List = [];
    state.cartGory1Id = id;
    state.cartGory2Id = "";
    state.cartGory3Id = "";
  },
  //二级选中触发
  CARTGORY3(state, { res, id }) {
    state.cartGory3List = res;
    state.cartGory2Id = id;
    state.cartGory3Id = "";
  },
  //第三个选项选中触发
  SET3ID(state, id) {
    state.cartGory3Id = id;
  },
  //SETATTR
  SETATTR(state, res) {
    // console.log(res);
    state.attrList = res;
  }
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
};
